﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web;
using System.Globalization;
using System.Web.Script.Serialization;


#region Assembly Resource Attribute

[assembly: WebResource("AjaxControlToolkit.SideBar.SideBar.js", "application/x-javascript")]
[assembly: WebResource("AjaxControlToolkit.SideBar.SideBar.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlToolkit.SideBar.active.gif", "image/gif")]
[assembly: WebResource("AjaxControlToolkit.SideBar.break.gif", "image/gif")]
[assembly: WebResource("AjaxControlToolkit.SideBar.caption.gif", "image/gif")]
[assembly: WebResource("AjaxControlToolkit.SideBar.sidebar.gif", "image/gif")]
[assembly: WebResource("AjaxControlToolkit.SideBar.pin.gif", "image/gif")]
[assembly: WebResource("AjaxControlToolkit.SideBar.unpin.gif", "image/gif")]

#endregion Assembly Resource Attribute


namespace AjaxControlToolkit
{
    //[Designer("AjaxControlToolkit.SliderContainerDesigner, AjaxControlToolkit")]
    [ParseChildren(typeof(SidePanel))]
    [RequiredScript(typeof(CommonToolkitScripts))]
    [ClientCssResource("AjaxControlToolkit.SideBar.SideBar.css")]
    [ClientScriptResource("AjaxControlToolkit.SideBar", "AjaxControlToolkit.SideBar.SideBar.js")]
    [System.Drawing.ToolboxBitmap(typeof(SideBar), "SideBar.SideBar.ico")]
    public class SideBar : ScriptControlBase
    {
        private string _defaultCSS = "ajax__sidebar";

        /// <summary>
        /// Initializes a new SliderContainer
        /// </summary>
        public SideBar()
            : base(true, HtmlTextWriterTag.Div)
        {
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public SidePanelCollection Panels
        {
            get { return (SidePanelCollection)Controls; }
        }

        [DefaultValue(-1)]
        [Category("Behavior")]
        [ExtenderControlProperty]
        [ClientPropertyName("activePanelIndex")]
        public virtual int ActivePanelIndex
        {
            get { return (int)(ViewState["ActivePanelIndex"] ?? 0); }
            set { ViewState["ActivePanelIndex"] = value; }
        }

        [DefaultValue(true)]
        [Category("Appearance")]
        [ExtenderControlProperty]
        [ClientPropertyName("autoHide")]
        public Boolean AutoHide
        {
            get { return (Boolean)(ViewState["AutoHide"] ?? true); }
            set { ViewState["AutoHide"] = value; }
        }

        [UrlProperty]
        [Category("Appearance")]
        [ExtenderControlProperty]
        [ClientPropertyName("unpinnedImage")]
        public string UnpinnedImage
        {
            get { return (string)(ViewState["UnpinnedImage"] ?? ""); }
            set { ViewState["UnpinnedImage"] = value; }
        }

        [UrlProperty]
        [Category("Appearance")]
        [ExtenderControlProperty]
        [ClientPropertyName("pinnedImage")]
        public string PinnedImage
        {
            get { return (string)(ViewState["PinnedImage"] ?? ""); }
            set { ViewState["PinnedImage"] = value; }
        }


        [DefaultValue("ajax__sidebar")]
        [Category("Appearance")]
        public override string CssClass
        {
            get { return base.CssClass; }
            set { base.CssClass = value; }
        }

        [DefaultValue(typeof(Unit), "36")]
        [Category("Appearance")]
        public Unit SideBarWidth
        {
            get { return (Unit)(ViewState["SideBarWidth"] ?? new Unit(36)); }
            set { ViewState["SideBarWidth"] = value; }
        }

        [DefaultValue(typeof(Unit), "24")]
        [Category("Appearance")]
        public Unit CaptionHeight
        {
            get { return (Unit)(ViewState["CaptionHeight"] ?? new Unit(24)); }
            set { ViewState["CaptionHeight"] = value; }
        }

        [DefaultValue(typeof(Unit), "36")]
        [Category("Appearance")]
        public override Unit Width
        {
            get
            {
                return SideBarWidth;
            }
            set
            {
                base.Width = value;
                SideBarWidth = value;
            }
        }

        [RequiredProperty]
        [DefaultValue(typeof(Unit), "150")]
        [Category("Appearance")]
        public Unit PanelsWidth
        {
            get { return (Unit)(ViewState["PanelsWidth"] ?? new Unit(150)); }
            set { ViewState["PanelsWidth"] = value; }
        }

        [DefaultValue(1000)]
        [Category("Appearance")]
        [ExtenderControlProperty]
        [ClientPropertyName("hideDelay")]
        public int HideDelay
        {
            get { return (int)(ViewState["HideDelay"] ?? 1000); }
            set { ViewState["HideDelay"] = value; }
        }

        [DefaultValue("")]
        [Category("Behavior")]
        [ExtenderControlEvent]
        [ClientPropertyName("activePanelChanged")]
        public string OnClientActivePanelChanged
        {
            get { return (string)(ViewState["OnClientActivePanelChanged"] ?? string.Empty); }
            set { ViewState["OnClientActivePanelChanged"] = value; }
        }

        [DefaultValue("")]
        [Category("Behavior")]
        [ExtenderControlEvent]
        [ClientPropertyName("autoHideChanged")]
        public string OnClientAutoHideChanged
        {
            get { return (string)(ViewState["OnClientAutoHideChanged"] ?? string.Empty); }
            set { ViewState["OnClientAutoHideChanged"] = value; }
        }


        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            Style.Remove(HtmlTextWriterStyle.Visibility);
            if (!ControlStyleCreated)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, _defaultCSS);
            }
            base.AddAttributesToRender(writer);
        }
        
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, SideBarWidth.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID + "_bar");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            
            RenderPanels(writer);

            writer.RenderEndTag();

            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Visibility, "hidden");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID + "_content");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, PanelsWidth.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID + "_caption");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, CaptionHeight.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddStyleAttribute("float", "left");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID + "_caption_label");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
            writer.AddStyleAttribute("float", "right");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID + "_caption_autohide");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.Src, AutoHide ? UnpinnedImage : PinnedImage);
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();

            RenderChildren(writer);

            writer.RenderEndTag();

        }

        protected virtual void RenderPanels(HtmlTextWriter writer)
        {
            foreach (SidePanel panel in Panels)
            {
                panel.RenderPanel(writer);
            }
        }

        protected override void AddParsedSubObject(object obj)
        {
            SidePanel objSidePanel = obj as SidePanel;
            if (null != objSidePanel)
            {
                Controls.Add(objSidePanel);
            }
            else if (!(obj is LiteralControl))
            {
                throw new HttpException(string.Format(CultureInfo.CurrentCulture, "SideBar cannot have children of type '{0}'.", obj.GetType()));
            }
        }

        protected override void AddedControl(Control control, int index)
        {
            ((SidePanel)control).SetOwner(this);
            base.AddedControl(control, index);
        }

        protected override void RemovedControl(Control control)
        {
            SidePanel controlSliderPanel = control as SidePanel;
            controlSliderPanel.SetOwner(null);
            base.RemovedControl(control);
        }

        protected override ControlCollection CreateControlCollection()
        {
            return new SidePanelCollection(this);
        }

        protected override Style CreateControlStyle()
        {
            SliderPanelContainerStyle style = new SliderPanelContainerStyle(ViewState);
            style.CssClass = _defaultCSS;
            return style;
        }

        protected override void LoadClientState(string clientState)
        {
            Dictionary<string, object> state = (Dictionary<string, object>)new JavaScriptSerializer().DeserializeObject(clientState);
            if (state != null)
            {
                ActivePanelIndex = (int)state["ActivePanelIndex"];
                AutoHide = (bool)state["AutoHide"];
            }
        }

        protected override string SaveClientState()
        {
            Dictionary<string, object> state = new Dictionary<string, object>();
            state["ActivePanelIndex"] = ActivePanelIndex;
            state["AutoHide"] = AutoHide;
            return new JavaScriptSerializer().Serialize(state);
        }


        private sealed class SliderPanelContainerStyle : Style
        {
            public SliderPanelContainerStyle(StateBag state)
                : base(state)
            {
            }

            protected override void FillStyleAttributes(CssStyleCollection attributes, IUrlResolutionService urlResolver)
            {
                base.FillStyleAttributes(attributes, urlResolver);

                attributes.Remove(HtmlTextWriterStyle.Height);
                attributes.Remove(HtmlTextWriterStyle.BackgroundColor);
                attributes.Remove(HtmlTextWriterStyle.BackgroundImage);
            }
        }
    }
}
